package com.gadgets.test.suite;


import com.gadgets.test.dto.CsvDataObject;
import com.gadgets.test.util.CsvSqlReader;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能说明：
 *
 * @author Unclexiu
 * @version V202101.00.000
 * @data 2022-09-21
 * 修改记录：  修改日期          修改人员    修改说明
 * 2022-09-21  hspcadmin   创建文件
 */
@SpringBootConfiguration
@EnableTransactionManagement(proxyTargetClass = true)
@ContextConfiguration
@SpringBootTest
@ComponentScan(value = "com.gadgets.test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class AbsTestSuite {

	protected final Logger logger = LoggerFactory.getLogger(AbsTestSuite.class);

	public String[] csvPaths = {};

	public List<CsvDataObject> csvDataObjects = new ArrayList<>();

	@Autowired
	private DataExcutor dataExcutor;

	@Autowired
	private WebApplicationContext context;

	@BeforeAll
	public void beforeTest() throws Exception {
		logger.info("开始初始化加载数据，连接池对象：{}", dataExcutor.toString());
		for (String csvPath : csvPaths) {
			CsvDataObject csvDataObject = CsvSqlReader.readCsv(csvPath);
			csvDataObjects.add(csvDataObject);
			List<String> list = CsvSqlReader.sqlInsertGenerator(csvDataObject);
			int i = dataExcutor.excuteGdagetsInsertSQL(list);
			if(i!=list.size()){
				logger.error("执行的SQL数量和影响记录数不一致");
			}
		}
	}
	@AfterAll
	public void afterTest() throws Exception {
		logger.info("开始清理测试数据");
		for (CsvDataObject csvDataObject : csvDataObjects) {
			List<String> list = CsvSqlReader.sqlTruncateGenerator(csvDataObject);
			dataExcutor.excuteGdagetsInsertSQL(list);
		}
	}
	protected ResultActions buildMockPostRequest(String url, MediaType mediaType, HashMap<String, String> param){
		MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
		MockHttpServletRequestBuilder requestBuilder =
				MockMvcRequestBuilders
						.post(url)
						.contentType(mediaType);
		for (Map.Entry<String, String> entry : param.entrySet()) {
			requestBuilder.param(entry.getKey(), entry.getValue());
		}
		try {
			return mockMvc.perform(requestBuilder);
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}

}
